<?php

/**
 * @author chenshubo
 * 平行云仓订单
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopycApi\ShopSameLevelYunCangApiClient;

class Shopsamelevelyuncang extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierGoodsInfo  = $param['supplierGoodsInfo'];
        $userGoodsList      = $supplierGoodsInfo['userGoodsList'];
        $goodsList          = $supplierGoodsInfo['goodsList'];
        $payDeclare         = $supplierGoodsInfo['payDeclare'];
        $supplierCode       = $supplierGoodsInfo['supplierCode'];
        $thirdPartyPlatformCode = $supplierGoodsInfo['thirdPartyPlatformCode'];
        $apiBody            = $supplierGoodsInfo['apiBody'];
        $parameter          = [];
        
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
        
        //按照商品组进行拆解
        foreach ($userGoodsList as $eval )
        {
            foreach ($eval as $orderGoods )
            {
                $goodsSku = $orderGoods['goodsSku'];
                $supplierCommodityType = $goodsList[$goodsSku]['supplierCommodityType'];
                $supplier_dsn = (string)IdService::instance()->getOtherID();
                if( $isDelare && !$isShop && $supplierCommodityType != GOODS_TYPE_YBMY ) {
                    $param['order']['supplier_dsn'] = $param['order']['orderSn'];
                    $param['order']['supplier_psn'] = $param['order']['orderSn'];
                } else {
                    $param['order']['supplier_dsn'] = $supplier_dsn;
                    $param['order']['supplier_psn'] = $supplier_dsn;
                }
                
                //是否属于最后一级云仓
                $param['order']['isNotTopSystem'] = 0;
                $param['order']['goods'] = [$orderGoods];
                
                //调用上游创建订单接口
                $res = $this->callSdkApi(ShopSameLevelYunCangApiClient::class,$apiBody)->createOrder($param);
                if( $res['code'] != Normal::SUC ) {
                    return $res;
                }
                
                $parameter[] = [
                    'tag'       => $thirdPartyPlatformCode ? $thirdPartyPlatformCode : $supplierCode,
                    'uid'       => $supplier_dsn,
                    'url'       => $res['data']['url'],
                    'data'      => json_encode($res['data']['data'],JSON_UNESCAPED_UNICODE),
                    'method'    => 'post',
                    'param'     => json_encode($param,JSON_UNESCAPED_UNICODE),
                    'format'    => $res['data']['format'],
                    'header'    => [],
                ];
            }
        }
        return Normal::returnCode(Normal::SUC,"获取订单参数成功",[
            'orderPush' => $parameter
        ]);
    }

    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopSameLevelYunCangApiClient::class,$param['apiBody'])->payOrder($param);
    }
    
    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return $this->callSdkApi(ShopSameLevelYunCangApiClient::class,$param['apiBody'])->cancelOrder($param);
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopSameLevelYunCangApiClient::class,$param['apiBody'])->orderAfterSale($param);
    }
    
    /**
     * 获取响应code
     * @param array $param
     */
    public function getCreateOrderCode( $param = [])
    {
        $result = @json_decode($param['result'],true);
        if( !isset($result['code']) || $result['code'] != Normal::SUC ) {
            $msg = isset($result['msg']) ? $result['msg'] : '请求接口失败';
            return Normal::returnCode(Normal::FAIL,$msg);
        }
        
        if( empty($result['data']['orderItems']) ) {
            return Normal::returnCode(Normal::FAIL,"创建订单失败（订单数据格式错误）");
        }
        
        $successOrders = $result['data']['orderItems'];
        $orderItems = [];
        foreach ($successOrders as $item )
        {
            $orderItems[] = [
                'supplier_csn' => $item['orderNo'],
                'orderGoods'   => $item['goods_items']
            ];
            unset($item);
        }
    
        return Normal::returnCode(Normal::SUC,"OK",[
            'orderList' => $orderItems,
        ]);
    }
}